package sam.learn.framework.security;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import sam.learn.biz.user.repo.entity.UserInfo;
import sam.learn.biz.user.service.UserInfoService;

import java.util.ArrayList;
import java.util.Objects;


@Service
public class JwtUserDetailsService implements UserDetailsService {

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = userInfoService.queryUserInfoBy(username);
        if (Objects.nonNull(userInfo)) {
            return new User(username, userInfo.getPassword(), new ArrayList<>());
        } else {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
    }
}
